package me.sulong;

import org.h2.jdbcx.JdbcDataSource;
import org.hibernate.ejb.HibernatePersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import javax.sql.DataSource;

@Configuration
public class BaseSpringConfig {
    @Bean
    DataSource dataSource () {
        JdbcDataSource ds = new JdbcDataSource();
        ds.setURL("jdbc:h2:˜/test");
        ds.setUser("sa");
        ds.setPassword("sa");
        return ds;
    }

    @Bean
    LocalContainerEntityManagerFactoryBean emf() {
        LocalContainerEntityManagerFactoryBean emfFactory = new LocalContainerEntityManagerFactoryBean();
        emfFactory.setPersistenceUnitName("default");
        emfFactory.setDataSource(dataSource());
        emfFactory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        emfFactory.setPersistenceProviderClass(HibernatePersistence.class);
        emfFactory.setPackagesToScan("me.sulong");
        return emfFactory;
    }

    @Bean
    JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    @Bean
    TransactionalOperation transactionalOperation() {
        return new TransactionalOperation();
    }

}
